import javax.net.ssl.SSLContext;
import java.util.HashMap;
import java.util.Map;

public class Solution5 {
    public static void main(String[] args) {
        String c = "ccrrooaakk";
        int ans = minNumberOfFrogs(c);
        System.out.println(ans);
    }

    public static int minNumberOfFrogs(String c) {
        String str = "croak";
        int n = str.length();
        char[] croakOfFrogs = c.toCharArray();
        Map<Character,Character> hash1 = new HashMap<>();//记录当前字符的前一个字符
        Map<Character,Integer> hash2 = new HashMap<>();//记录当前字符的数量
        hash2.put(croakOfFrogs[0],0);
        for(int i = 1; i < n; i++) {
            hash1.put(str.charAt(i), str.charAt(i - 1));
            hash2.put(str.charAt(i),0);
        }

        for(int i = 0; i < croakOfFrogs.length; i++) {
            char ch = croakOfFrogs[i];
            if(ch == 'c') {
                if(hash2.get('k') == 0) {
                    hash2.put(ch,hash2.get(ch) + 1);
                }else {
                    hash2.put('k',hash2.get('k') - 1);
                    hash2.put(ch, hash2.get(ch) + 1);
                }
            }else {
                char prech = hash1.get(ch);
                if(hash2.get(prech) != 0){
                    hash2.put(prech, hash2.get(prech) - 1);
                    hash2.put(ch , hash2.get(ch) + 1);
                }else {
                    return -1;
                }
            }
        }
        for(int i = 0; i < n - 1; i++) {
            char ch = str.charAt(i);
            if(hash2.get(ch) != 0) {
                return -1;
            }
        }
        return hash2.get('k');
    }

    public static int minNumberOfFrogs2(String c) {
        String t = "croak";
        int n = t.length();
        char[] croakOfFrogs = c.toCharArray();
        int[] hash = new int[n]; // 数组模拟哈希表
        //[x, x这个字符对应的下标]
        Map<Character, Integer> index = new HashMap<>();
        for(int i = 0; i < n; i++) {
            index.put(t.charAt(i), i);
        }
        for(char ch : croakOfFrogs) {
            if(ch == t.charAt(0)) {
                if(hash[n - 1] != 0) {
                    hash[n - 1]--;
                }
                hash[0]++;
            }else {
                int i = index.get(ch);
                if(hash[i - 1] == 0) {
                    return -1;
                }
                hash[i - 1]--;
                hash[i]++;
            }
        }

        for(int i = 0; i < n - 1; i++) {
            if(hash[i] != 0) {
                return -1;
            }
        }
        return hash[n - 1];
    }
}
